<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      python/MySQL入门/5-Mac安装MySQL | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>python/MySQL入门/5-Mac安装MySQL</h2>



  <p class="post-date">2019-11-07</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="一-下载"><a href="#一-下载" class="headerlink" title="一 下载"></a>一 下载</h1><p><a href="https://downloads.mysql.com/archives/community/" target="_blank" rel="noopener">地址</a></p>
<p><img src="https://tva1.sinaimg.cn/large/0081Kckwgy1gjzcpq9rhfj31bg0u0495.jpg" alt="image-20201023163333726"></p>
<h1 id="二-安装"><a href="#二-安装" class="headerlink" title="二 安装"></a>二 安装</h1><p>一路下一步即可</p>
<h1 id="三-环境配置"><a href="#三-环境配置" class="headerlink" title="三 环境配置"></a>三 环境配置</h1><h2 id="3-2-加入环境变量"><a href="#3-2-加入环境变量" class="headerlink" title="3.2 加入环境变量"></a>3.2 加入环境变量</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> open ~/.bash_profile</span><br><span class="line"> <span class="comment">#把下面两行贴入，保存关闭</span></span><br><span class="line">export PATH=$PATH:/usr/local/mysql/bin</span><br><span class="line">export PATH=$PATH:/usr/local/mysql/support-files</span><br><span class="line"><span class="comment">#让配置生效</span></span><br><span class="line">source ~/.bash_profile </span><br><span class="line"><span class="comment">#查看环境变量</span></span><br><span class="line">echo $PATH</span><br></pre></td></tr></table></figure>

<h2 id="3-2-开启，关闭，查看服务"><a href="#3-2-开启，关闭，查看服务" class="headerlink" title="3.2 开启，关闭，查看服务"></a>3.2 开启，关闭，查看服务</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 停止MySQL服务</span></span><br><span class="line">sudo mysql.server stop</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重启MySQL服务</span></span><br><span class="line">sudo mysql.server restart</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看MySQL服务状态</span></span><br><span class="line">sudo mysql.server status</span><br></pre></td></tr></table></figure>

<h1 id="四-修改默认root密码"><a href="#四-修改默认root密码" class="headerlink" title="四 修改默认root密码"></a>四 修改默认root密码</h1><p>正常进入的话，执行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set password = password(<span class="string">'root'</span>);</span><br></pre></td></tr></table></figure>

<p>不能正常进入</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/local/mysql/bin/</span><br><span class="line"><span class="comment">#跳过校验，启动项目</span></span><br><span class="line">sudo ./mysqld_safe --skip-grant-tables</span><br><span class="line"><span class="comment">#重新打开一个终端输入命令如下：</span></span><br><span class="line">cd /usr/local/mysql/bin/</span><br><span class="line">./mysql</span><br><span class="line"><span class="comment">#输入完以上命令就可以进入到mysql命令状态下了，重新设置密码为‘123’：</span></span><br><span class="line">FLUSH PRIVILEGES;</span><br><span class="line">SET PASSWORD FOR <span class="string">'root'</span>@<span class="string">'localhost'</span> = PASSWORD(<span class="string">'123'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">#远程访问</span></span><br><span class="line">GRANT ALL PRIVILEGES ON *.* TO root@<span class="string">"%"</span> IDENTIFIED BY <span class="string">"root"</span>;    //为root添加远程连接的能力  </span><br><span class="line">flush privileges;  </span><br><span class="line">select host,user <span class="keyword">from</span> user; //查看修改是否成功。</span><br></pre></td></tr></table></figure>

<h1 id="五-配置"><a href="#五-配置" class="headerlink" title="五 配置"></a>五 配置</h1><p>进入到 /usr/local/mysql/support-files 目录。创建my.cnf，内容如下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[mysqld]</span><br><span class="line">default-storage-engine&#x3D;INNODB</span><br><span class="line">character-set-server&#x3D;utf8</span><br><span class="line">port &#x3D; 3306</span><br><span class="line"></span><br><span class="line">[client]</span><br><span class="line">default-character-set&#x3D;utf8</span><br></pre></td></tr></table></figure>

<h1 id="六-卸载"><a href="#六-卸载" class="headerlink" title="六 卸载"></a>六 卸载</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、 rpm -qa | grep -i mysql</span><br><span class="line">查找已经安装的mysql.</span><br><span class="line">MySQL-server<span class="number">-5.6</span><span class="number">.43</span><span class="number">-1.</span>el6.x86_64 </span><br><span class="line">MySQL-client<span class="number">-5.6</span><span class="number">.43</span><span class="number">-1.</span>el6.x86_64</span><br><span class="line">MySQL-devel<span class="number">-5.6</span><span class="number">.43</span><span class="number">-1.</span>el6.x86_64</span><br><span class="line">以上三个就是我安装的mysql.</span><br><span class="line"><span class="number">2</span>、 yum -y remove mysql-*</span><br><span class="line">网上的一般用rpm -e 的命令删除mysql,这样表面上删除了mysql,可是mysql的一些残余程序仍然存在,并且通过第一步的方式也查找不到残余,而yum命令比较强大,可以完全删除mysql.(ps:用rpm删除后再次安装的时候会提示已经安装了,这就是rpm没删除干净的原因)</span><br><span class="line"><span class="number">3</span>、 find / -name mysql</span><br><span class="line">查找mysql的一些目录,把所有出现的目录统统删除.可以使用rm -rf  路径，删除时请注意，一旦删除无法恢复。</span><br><span class="line"><span class="number">4</span>、rm -rf /etc/my.cnf</span><br><span class="line">这个是删除配置文件</span><br><span class="line"><span class="number">5</span>、 rm -rf /root/.mysql_sercret</span><br><span class="line">删除mysql的默认密码,如果不删除,以后安装mysql这个sercret中的默认密码不会变,使用其中的默认密码就可能会报类似Access denied <span class="keyword">for</span> user <span class="string">'root@localhost'</span> (using password:yes)的错误.</span><br><span class="line">五步完成之后,这样mysql就全部删除干净了.</span><br><span class="line"><span class="comment">#centos 下安装</span></span><br><span class="line"><span class="comment">#下载安装包文件</span></span><br><span class="line">wget http://repo.mysql.com/mysql-community-release-el7<span class="number">-5.</span>noarch.rpm</span><br><span class="line"><span class="comment">#安装mysql-community-release-el7-5.noarch.rpm包</span></span><br><span class="line">rpm -ivh mysql-community-release-el7<span class="number">-5.</span>noarch.rpm</span><br><span class="line"><span class="comment">#安装完成之后，会在 /etc/yum.repos.d/ 目录下新增 mysql-community.repo 、mysql-community-source.repo 两个 yum 源文件</span></span><br><span class="line"><span class="comment">#执行 yum repolist all | grep mysql 命令查看可用的 mysql 安装文件</span></span><br><span class="line">yum install mysql-server</span><br><span class="line"><span class="comment">#检查mysql是否安装成功。</span></span><br><span class="line">rpm -qa | grep mysql</span><br><span class="line"><span class="comment">#启动mysql服务</span></span><br><span class="line">systemctl start mysqld.service</span><br><span class="line"><span class="comment">#设置密码 。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#mysql5.6 安装完成后，它的 root 用户的密码默认是空的，我们需要及时用 mysql 的 root 用户登录（第一次直接回车，不用输入密码），并修改密码。</span></span><br><span class="line">mysql -u root</span><br><span class="line">mysql&gt; use mysql;</span><br><span class="line">mysql&gt; update user set password=PASSWORD(<span class="string">"123"</span>) where User=<span class="string">'root'</span>;</span><br><span class="line">mysql&gt; flush privileges; </span><br><span class="line"><span class="comment">#设置远程主机登录</span></span><br><span class="line">mysql&gt; GRANT ALL PRIVILEGES ON *.* TO <span class="string">'root'</span>@<span class="string">'%'</span> IDENTIFIED BY <span class="string">'123'</span>;</span><br><span class="line"><span class="comment">#执行以下命令，为root 用户添加远程登录的能力。</span></span><br><span class="line">mysql&gt; GRANT ALL PRIVILEGES ON *.* TO root@<span class="string">"%"</span> IDENTIFIED BY <span class="string">"123"</span>;</span><br><span class="line"></span><br><span class="line">yum install mysql-devel</span><br><span class="line">yum install python36-devel</span><br><span class="line">yum install python-devel</span><br><span class="line">pip install mysqlclient</span><br></pre></td></tr></table></figure>

</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/%E8%B7%AF%E9%A3%9E%E9%A1%B9%E7%9B%AE/1-%E9%A1%B9%E7%9B%AE%E5%9F%BA%E7%A1%80/2-%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83%E7%9A%84%E6%90%AD%E5%BB%BA/">
        <span class="nav-arrow">← </span>
        
          python/路飞项目/1-项目基础/2-虚拟环境的搭建
        
      </a>
    
    
      <a class="nav-right" href="/python/%E8%B7%AF%E9%A3%9E%E9%A1%B9%E7%9B%AE/1-%E9%A1%B9%E7%9B%AE%E5%9F%BA%E7%A1%80/8-luffy%E5%89%8D%E5%8F%B0%E9%85%8D%E7%BD%AE/">
        
          python/路飞项目/1-项目基础/8-luffy前台配置
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#一-下载"><span class="toc-nav-text">一 下载</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#二-安装"><span class="toc-nav-text">二 安装</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#三-环境配置"><span class="toc-nav-text">三 环境配置</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-2-加入环境变量"><span class="toc-nav-text">3.2 加入环境变量</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-2-开启，关闭，查看服务"><span class="toc-nav-text">3.2 开启，关闭，查看服务</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#四-修改默认root密码"><span class="toc-nav-text">四 修改默认root密码</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#五-配置"><span class="toc-nav-text">五 配置</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#六-卸载"><span class="toc-nav-text">六 卸载</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/python/MySQL入门/5-Mac安装MySQL/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "python/MySQL入门/5-Mac安装MySQL",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>